﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.基础数据
{
    /// <summary>
    /// 字典项
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class DictItemController : BaseController
    {
        private readonly IDictItemService dictItemService;
        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="dictItemService"></param>
        public DictItemController(IWebHostEnvironment environment, IDictItemService dictItemService) : base(environment)
        {
            this.dictItemService = dictItemService;
        }

        /// <summary>
        /// 查询所有字典项
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllDictItemList(string? keyword, long typeId, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = dictItemService.GetAllDictItemList(keyword, typeId, pageIndex, pageSize, out var totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 根据id查询字典项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDictItemById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = dictItemService.GetDictItemById(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }
        /// <summary>
        /// 根据字典类型id查询字典项
        /// </summary>
        /// <param name="dicTypeId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDictItemByDicTypeId(int dicTypeId, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = dictItemService.GetDictItemByDicTypeId(dicTypeId, pageIndex, pageSize, out var totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
        /// <summary>
        /// 字典项树
        /// </summary>
        /// <param name="itemName"></param>
        /// <param name="typeId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDictItemTreeByitemName(string? itemName, int typeId)
        {
            var res = dictItemService.GetDictItemTreeByitemName(itemName, typeId);
            return Ok(BaseResult.Result(res, "", "200"));
        }
        /// <summary>
        /// 查询全部
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetDictItems()
        {
            return Ok(BaseResult.Result(dictItemService.GetDictItems(), "", "200"));
        }
        /// <summary>
        /// 添加字典项
        /// </summary>
        /// <param name="dictItem"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddDictItem(DictItem dictItem)
        {       
            dictItem.CreateUserId = base.BaseGetNowUserId();
            dictItem.CreateUserName = base.BaseGetNowUserName();
            string message = "";
            var result = dictItemService.AddDictItem(dictItem, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！编码已存在！", "200"));
        }
        /// <summary>
        /// 修改字典项
        /// </summary>
        /// <param name="dictItem"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdDictItem(DictItem dictItem)
        {
            if (dictItem.ItemName == "合格" || dictItem.ItemName == "不合格" || dictItem.ItemName == "待检")
            {
                return Ok(BaseResult.Result("400", dictItem.ItemName + "为系统状态不可更改！"));
            }
            if (dictItem.ItemName == "自产入库" || dictItem.ItemName == "外部调拨入库")
            {
                return Ok(BaseResult.Result("400", dictItem.ItemName + "为系统状态不可更改！"));
            }
            if (dictItem.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
              
            string message = "";
            dictItem.ModifyUserId = base.BaseGetNowUserId();
            dictItem.ModifyUserName = base.BaseGetNowUserName();
            var result = dictItemService.UpdDictItem(dictItem, out message);
            if (result)
            {
                return Ok(BaseResult.Result(result, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "修改失败！编码已存在！", "500"));
        }
        /// <summary>
        /// 删除字典
        /// </summary>
        /// <param name="id">字典id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelDictItem(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var dic = dictItemService.GetDictItemById(id);
            if (dic != null && (dic.ItemName == "合格" || dic.ItemName == "不合格" || dic.ItemName == "待检品"))
            {
                return Ok(BaseResult.Result("400", dic.ItemName + "为系统状态不可删除！"));
            }
            if (dic != null && (dic.ItemName == "自产入库" || dic.ItemName == "外部调拨入库"))
            {
                return Ok(BaseResult.Result("400", dic.ItemName + "为系统状态不可删除！"));
            }
            var IsOK = dictItemService.DelDictItem(id);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }
        /// <summary>
        /// 批量删除字典项
        /// </summary>
        /// <param name="idList">字典id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelDictItemList(long[] idList)
        {
            if (idList.Length <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var daRes = true;
            foreach (var id in idList)
            {
                var dic = dictItemService.GetDictItemById(id);
                if (dic != null && (dic.ItemName == "合格" || dic.ItemName == "不合格" || dic.ItemName == "待检品"))
                {
                    return Ok(BaseResult.Result("400", dic.ItemName + "为系统状态不可删除！"));
                }
                var res = dictItemService.DelDictItem(id);
                if (!res)
                {
                    daRes = false;
                    break;
                }
            }
            if (daRes)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 字典项树形展示
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="typeid"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMenuLists(int pageIndex = 1, int pageSize = 10, int typeid = 0, string? name = "")
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            int totalCount;
            var list = dictItemService.GetMenuLists(pageIndex, pageSize, out totalCount, typeid, name);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
